package com.orhon.smartcampus.modules.teacher.fetcher;


import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class AllTeachersFetcher implements DataFetcher {

	@Autowired
	TInformationDao tInformationDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);

		List<GQLTeacher> GQLTeacher = new ArrayList<>();


		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from TInformation as teacher where deleted_at = null ";
		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("name")!=null) {
				hql += "and teacher.teacherName" + Util.decide(map.get("name"));
			}
			if(map.get("idcard")!=null) {
				hql += "and teacher.idcard " +Util.decide( map.get("idcard"));
			}

			if(map.get("id")!=null) {
				hql += "and teacher.id " +Util.decide( map.get("id"));
			}
			if(map.get("user_id")!=null) {
				hql += "and teacher.user_id " +Util.decide( map.get("user_id"));
			}
			if(map.get("gender")!=null) {
				hql += "and teacher.gender " +Util.decide( map.get("gender"));
			}
			if(map.get("birthday")!=null) {
				hql += "and teacher.birthday " +Util.decide( map.get("birthday"));
			}
			if(map.get("native_province_id")!=null) {
				hql += "and teacher.nativeprovince " +Util.decide( map.get("native_province_id"));
			}
			if(map.get("native_city_id")!=null) {
				hql += "and teacher.nativecity " +Util.decide( map.get("native_city_id"));
			}
			if(map.get("native_county_id")!=null) {
				hql += "and teacher.nativecounty " +Util.decide( map.get("native_county_id"));
			}
			if(map.get("birth_province_id")!=null) {
				hql += "and teacher.birthprovince " +Util.decide( map.get("birth_province_id"));
			}
			if(map.get("birth_city_id")!=null) {
				hql += "and teacher.birthcity " +Util.decide( map.get("birth_city_id"));
			}
			if(map.get("birth_county_id")!=null) {
				hql += "and teacher.birthcounty " +Util.decide( map.get("birth_county_id"));
			}
			if(map.get("type")!=null) {
				hql += "and teacher.type " +Util.decide( map.get("type"));
			}
			if(map.get("email")!=null) {
				hql += "and teacher.email " +Util.decide( map.get("email"));
			}
			if(map.get("telnumber")!=null) {
				hql += "and teacher.telnumber " +Util.decide( map.get("telnumber"));
			}
			if(map.get("nationality")!=null) {
				hql += "and teacher.nationality " +Util.decide( map.get("nationality"));
			}
			if(map.get("political_type")!=null) {
				hql += "and teacher.politicaltype " +Util.decide( map.get("political_type"));
			}
			if(map.get("work_status")!=null) {
				hql += "and teacher.workstatus " +Util.decide( map.get("work_status"));
			}
			if(map.get("job_title")!=null) {
				hql += "and teacher.jobtitle " +Util.decide( map.get("job_title"));
			}
			if(map.get("education")!=null) {
				hql += "and teacher.education " +Util.decide( map.get("education"));
			}
			if(map.get("degree")!=null) {
				hql += "and teacher.degree " +Util.decide( map.get("degree"));
			}
			if(map.get("master_supervisor")!=null) {
				hql += "and teacher.mastersupervisor " +Util.decide( map.get("master_supervisor"));
			}
			if(map.get("teach")!=null) {
				hql += "and teacher.teach " +Util.decide( map.get("teach"));
			}
			if(map.get("cmbprovince3_id")!=null) {
				hql += "and teacher.cmbprovince3 " +Util.decide( map.get("cmbprovince3_id"));
			}
			if(map.get("cmbcity3_id")!=null) {
				hql += "and teacher.cmbcity3 " +Util.decide( map.get("cmbcity3_id"));
			}

			if(map.get("cmbcounty3_id")!=null) {
				hql += "and teacher.cmbcounty3 " +Util.decide( map.get("cmbcounty3_id"));
			}

			if(map.get("cmbprovince2_id")!=null) {
				hql += "and teacher.cmbprovince2 " +Util.decide( map.get("cmbprovince2_id"));
			}

			if(map.get("cmbcity2_id")!=null) {
				hql += "and teacher.cmbcity2 " +Util.decide( map.get("cmbcity2_id"));
			}

			if(map.get("cmbcounty2_id")!=null) {
				hql += "and teacher.cmbcounty2 " +Util.decide( map.get("cmbcounty2_id"));
			}

			if(map.get("school_id")!=null) {
				hql += "and teacher.baseSchoolsBySchoolId " +Util.decide( map.get("school_id"));
			}

			if(map.get("nation")!=null) {
				hql += "and teacher.nation " +Util.decide( map.get("nation"));
			}

			if(map.get("imgphotos")!=null) {
				hql += "and teacher.imgphotos " +Util.decide( map.get("imgphotos"));
			}

			if(map.get("signimg")!=null) {
				hql += "and teacher.signimg " +Util.decide( map.get("signimg"));
			}


			if(map.get("introduction")!=null) {
				hql += "and teacher.introduction " +Util.decide( map.get("introduction"));
			}
			if(map.get("graders_id")!=null) {
				hql += "and teacher.id IN (select teacher_id from TeacherAndGrades where grades_id "+Util.decide( map.get("graders_id"))+") ";
				System.out.println("年级ID:"+map.get("graders_id")+"<><><>><><>年级查询语句:"+hql);

			}

			if(map.get("subjects_id")!=null) {
				hql += "and teacher.id IN (select teacher_id from TeacherAndSubjects where subjects_id "+Util.decide( map.get("subjects_id"))+") ";
			}

			if(map.get("periods_id")!=null) {
				hql += "and teacher.id IN (select teacher_id from TeacherAndPeriods where periods_id "+Util.decide( map.get("periods_id"))+") ";
			}



			if(map.get("graders_id")!=null) {
				hql += "and teacher.id IN (select teacher_id from TeacherAndGrades where grades_id "+Util.decide( map.get("graders_id"))+") ";
			}

			if(map.get("subjects_id")!=null) {
				hql += "and teacher.id IN (select teacher_id from TeacherAndSubjects where subjects_id "+Util.decide( map.get("subjects_id"))+") ";
			}

			if(map.get("periods_id")!=null) {
				hql += "and teacher.id IN (select teacher_id from TeacherAndPeriods where periods_id "+Util.decide( map.get("periods_id"))+") ";
			}

		}
		hql += " ORDER BY created_at DESC";
		List<TInformation> list = tInformationDao.find(hql,null,page.getPage(),page.getLimit());
		System.out.println("教职工总数:"+list.size());



		for (TInformation item : list) {
			GQLSchool gqlSchool = null;
			if (env.getSelectionSet().contains("data/school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}

			GQLUser gqlUser = null;
			if (env.getSelectionSet().contains("data/user")) {
				Users users = item.getUserUsersByUserId();
				gqlUser = UserMapper.MAPPER.mapUser(users);
			}
			GQLTeacher teacher = TeacherMapper.MAPPER.toTeacher(item);
			teacher.setUser(gqlUser);
			teacher.setSchool(gqlSchool);
			GQLTeacher.add(teacher);
		}

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = tInformationDao.getTotalCount(hql);
		}
		return Result.ok().put("data", GQLTeacher).put("total", total);
	}
}